# _*_ coding:utf-8 _*_
# @Time  : 2023.07.25
# @Author: zizlee
from fastapi import APIRouter, Depends
from response import APIResponse
from utils.jwt_token import TokenPayload
from db_utils import BaseDatabaseConnection

""" 已弃用，菜单不做灵活处理 """
class MenuApi:
    DATA_USER = 1  # 数据用户(有数据库+定制数据库)
    FULL_DOSE = 2  # 全量用户
    FLEX_USER = 3  # 定制用户

    PUBLIC_MENUS = [
        {'id': 1, 'name': '资讯速递', 'code': '1000', 'path': '/information', 'hasChildren': False, 'subShow': False},
        {'id': 2, 'name': '市场策略', 'code': '2000', 'path': '/strategy', 'hasChildren': False, 'subShow': False},
        {'id': 3, 'name': '研报文章', 'code': '3000', 'path': '/reports', 'hasChildren': False, 'subShow': False},
    ]
    FULL_DOSE_MENUS = [{'id': 4, 'name': '数据库', 'code': '4000', 'path': '/datalib', 'hasChildren': False, 'subShow': False}]
    FLEX_USER_MENUS = [{'id': 5, 'name': '数据库', 'code': '5000', 'path': '/flexDatalib', 'hasChildren': False, 'subShow': False}]
    DATA_USER_MENUS = [
        {'id': 4, 'name': '数据库', 'code': '4000', 'path': '/datalib', 'hasChildren': False, 'subShow': False},
        {'id': 5, 'name': '定制数据库', 'code': '5000', 'path': '/flexDatalib', 'hasChildren': False, 'subShow': False}
    ]

    MENUS = {
        1: PUBLIC_MENUS + DATA_USER_MENUS,
        2: PUBLIC_MENUS + FULL_DOSE_MENUS,
        3: PUBLIC_MENUS + FLEX_USER_MENUS
    }

    def __init__(self, user_id: int):
        self.user_id = user_id

    def user_navigation(self):
        with BaseDatabaseConnection() as conn:
            conn.cursor.execute("SELECT id,user_type FROM ruizhi_user WHERE id=%s LIMIT 1;", [self.user_id])
            user = conn.cursor.fetchone()
        if not user:
            return APIResponse.success(data={'navigation': []})
        navigation = self.MENUS.get(user['user_type'], [])
        return APIResponse.success(data={'navigation': navigation})


menu_api = APIRouter()


@menu_api.get('/navigation/')  # 获取用户的顶部菜单栏【本接口已弃用】菜单不做灵活
def user_navigation(payload: TokenPayload = Depends(TokenPayload)):
    return MenuApi(payload.user_id()).user_navigation()
